package com.wy.controller.wy;

import com.wy.bean.FeeBean;
import com.wy.model.sys.PageResult;
import com.wy.model.sys.UserEntity;
import com.wy.service.sys.UserService;
import com.wy.service.wy.FeeService;
import com.wy.utils.BaseResponseInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Auther: tracy
 * @Date: 2018/8/20 16:58
 * @Description: 费用管理
 */
@RestController
@RequestMapping("/fee")
@Api(value="API - FeeController",tags={"费用管理"})
public class FeeController {

    private Logger log = LoggerFactory.getLogger(FeeController.class);

    @Resource(name = "feeServiceImpl")
    private FeeService feeService;

    @Resource(name = "userServiceImpl")
    private UserService userService;

    @GetMapping("/feeRule")
    @ApiOperation(value="feeRule",notes="获取费用规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false,dataType = "Int", defaultValue = "10"),
            @ApiImplicitParam(name = "page", value = "当前页数", required = false,dataType = "Int",defaultValue = "1"),
            @ApiImplicitParam(name = "residenceId", value = "小区名称", required = false,dataType = "Int",defaultValue = "1"),
            @ApiImplicitParam(name = "feeName", value = "费用名称", required = false,dataType = "String",defaultValue = ""),
            @ApiImplicitParam(name = "feeType", value = "费用类型", required = false,dataType = "String",defaultValue = "")
    })
    public PageResult feeRuleList(Integer pageSize, Integer page,Integer residenceId,String feeName,String feeType,HttpServletRequest request) {
        List<String> rids = new ArrayList<>();
        UserEntity userEntity = userService.getUserByLoginName(request.getUserPrincipal().getName());
        if(null != userEntity) {
            String rid = userEntity.getRid();
            if(!StringUtils.isEmpty(rid)){
                List list = Arrays.asList(rid.split(","));
                rids = new ArrayList(list);
            }
        }
        PageResult pageResult = new PageResult();
        if("admin".equals(userEntity.getLoginName())){
        }else {
            rids.add("-1");//手动添加-1，如果当前登录用户无权限，则区别于admin的用户，查不到数据
        }

        pageResult.setData(feeService.feeRuleList(pageSize, page * pageSize,residenceId, feeName,feeType, rids));
        pageResult.setTotalCount(feeService.feeRuleSize(pageSize, page * pageSize, residenceId,feeName,feeType, rids));
        return pageResult;
    }

    @GetMapping("/feeRecord")
    @ApiOperation(value="feeRecord",notes="获取费用记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false,dataType = "Int", defaultValue = "10"),
            @ApiImplicitParam(name = "page", value = "当前页数", required = false,dataType = "Int",defaultValue = "1"),
            @ApiImplicitParam(name = "residenceId", value = "小区ID", required = false,dataType = "Int",defaultValue = "1"),
            @ApiImplicitParam(name = "feeName", value = "费用名称", required = false,dataType = "String",defaultValue = ""),
            @ApiImplicitParam(name = "feeStatus", value = "费用状态", required = false,dataType = "String",defaultValue = ""),
            @ApiImplicitParam(name = "feeType", value = "费用类型", required = false,dataType = "Int",defaultValue = ""),
            @ApiImplicitParam(name = "houseId", value = "房屋类型", required = false,dataType = "Int",defaultValue = ""),
            @ApiImplicitParam(name = "buildingId", value = "房屋类型", required = false,dataType = "Int",defaultValue = "")
    })
    public PageResult feeRecordList(HttpServletRequest request,Integer pageSize, Integer page,Integer residenceId,String feeName,String feeStatus,
                                    String feeType,Integer houseId,Integer buildingId) {
        List<String> rids = new ArrayList<>();
        UserEntity userEntity = userService.getUserByLoginName(request.getUserPrincipal().getName());
        if(null != userEntity) {
            String rid = userEntity.getRid();
            if(!StringUtils.isEmpty(rid)){
                List list = Arrays.asList(rid.split(","));
                rids = new ArrayList(list);
            }
        }
        PageResult pageResult = new PageResult();
        if("admin".equals(userEntity.getLoginName())){
        }else {
            rids.add("-1");//手动添加-1，如果当前登录用户无权限，则区别于admin的用户，查不到数据
        }

        pageResult.setData(feeService.feeRecordList(pageSize, page * pageSize,residenceId, feeName,feeStatus,feeType,houseId, rids,buildingId));
        pageResult.setTotalCount(feeService.feeRecordSize(pageSize, page * pageSize, residenceId, feeName,feeStatus,feeType,houseId, rids,buildingId));
        return pageResult;
    }


    @GetMapping("/records")
    @ApiOperation(value="records",notes="获取个人费用记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feeStatus", value = "费用状态", required = false,dataType = "String",defaultValue = ""),
            @ApiImplicitParam(name = "feeType", value = "费用类型", required = false,dataType = "String",defaultValue = ""),
            @ApiImplicitParam(name = "houseId", value = "房屋类型", required = true,dataType = "String",defaultValue = "")
    })
    public BaseResponseInfo feeRecordList(String feeStatus,String feeType, Integer houseId) {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = feeService.feeRecordList(feeStatus,feeType,houseId);
        return baseResponseInfo;
    }

    /**
     * 新建规则
     * @param feeBean
     * @return
     */
    @PostMapping("/feeRule")
    @ApiOperation(value="feeRule",notes="新建规则")
    public BaseResponseInfo insertFeeRule(@RequestBody FeeBean feeBean) {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        if(StringUtils.isEmpty(feeBean.getFeeName())) {
            baseResponseInfo.data = "费用名称不能为空！";
            return baseResponseInfo;
        }
        feeBean.setCreateTime(new Date());
        feeBean.setCreateUser("admin");
        feeBean.setExt5("1"); //状态
        feeService.insertFeeRule(feeBean);
        log.debug("The method is ending");
        return baseResponseInfo;
    }


    /**
     * 新建规则
     * @param feeBean
     * @return
     */
    @PutMapping("/feeRule/{id}")
    @ApiOperation(value="feeRule",notes="新建规则")
    public BaseResponseInfo insertFeeRule(@RequestBody FeeBean feeBean, @PathVariable int id) {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        feeService.updateFeeRule(feeBean);
        log.debug("The method is ending");
        return baseResponseInfo;
    }

    /**
     * 删除规则
     * @param groupId
     * @return
     */
    @DeleteMapping("/feeRule")
    @ApiOperation(value="feeRule",notes="删除规则")
    public BaseResponseInfo deleteFeeRule(@RequestBody List<String> groupId) {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        try {
            feeService.deleteFeeRule(groupId);
        }catch (Exception ex) {
            baseResponseInfo.code = 500;
            baseResponseInfo.data = ex.getMessage();
        }
        log.debug("The method is ending");
        return baseResponseInfo;
    }



    /**
     * 生成记录
     * @param
     * @return
     */
    @GetMapping("/generate")
    @ApiOperation(value="generate",notes="生成记录")
    public BaseResponseInfo generateFeeRecord() {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        feeService.generateFeeRecord(feeService.getFeeRuleById(8));
        log.debug("The method is ending");
        return baseResponseInfo;
    }



//    /**
//     * 修改设备信息
//     * @param feeBean
//     * @param id
//     * @return
//     */
//    @PutMapping("/{id}")
//    public BaseResponseInfo updateFeeRule(@RequestBody FeeBean feeBean, @PathVariable int id) {
//        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
////        feeService.updateFeeRule(deviceEntity);
//        log.debug("The method is ending");
//        return baseResponseInfo;
//    }
//
//    /**
//     * 删除设备信息
//     *
//     * @param groupId
//     * @return
//     */
//    @DeleteMapping("")
//    public void deleteDevices(@RequestBody List<String> groupId) {
//        deviceService.deleteDevices(groupId);
//    }
//
//    /**
//     * 根据小区id获取门禁设备
//     * @param rid
//     * @return
//     */
//    @GetMapping("/getDeviceByRid")
//    public BaseResponseInfo getDeviceByRid(Integer rid) {
//        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
//        List<DeviceEntity> deviceEntities = deviceService.getDeviceByRid(rid);
//        baseResponseInfo.data = deviceEntities;
//        return baseResponseInfo;
//    }
//
//    /**
//     * 指定设备id开门
//     */
//    @GetMapping("/openDoor/{id}")
//    public BaseResponseInfo openDoor(@PathVariable int id){
//        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
//        baseResponseInfo.data = deviceService.openDoor(id);
//        return baseResponseInfo;
//    }
//
//
//    @GetMapping("/download")
//    public void download(HttpServletRequest request, HttpServletResponse response,Integer residenceId, String deviceName) throws Exception {
////        response.setHeader("content-Type", "application/vnd.ms-excel");
////        response.setHeader("Content-Disposition", "attachment;filename="+new String( "设备信息.xls".getBytes("gb2312"), "ISO8859-1" ));
////        List<String> rids = new ArrayList<>();
////        UserEntity userEntity = userService.getUserByLoginName(username);
////        if(null != userEntity) {
////            String rid = userEntity.getRid();
////            if(!StringUtils.isEmpty(rid)){
////                rids = Arrays.asList(rid.split(","));
////            }
////        }
////        List<Residence> list = new ArrayList<>();
////        if("admin".equals(username)) {
////            list = deviceService.deviceList(null, null,residenceId,deviceName);
////        }else {
////            rids.add("-1");//手动添加-1，如果当前登录用户无权限，则区别于admin的用户，查不到数据
////            list = residenceService.residenceList1(null, null, projectName, rids);
////        }
////        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Residence.class, list);
////        workbook.write(response.getOutputStream());
//    }
//
//    /**
//     * 数据导入
//     * @param file
//     */
//    @PostMapping("/import")
//    public Map<String,String> importExcel(MultipartFile file){
//        Map<String,String> map = FileUtil.judgeRepeatExcel(file,2,1);
////        if(map.isEmpty()) {
////            List<Residence> residenceList = FileUtil.importExcel(file, 0, 1, Residence.class);
////
////            if (residenceList != null) {
////                System.out.println("导入数据一共【" + residenceList.size() + "】行");
////               List<Residence> temp = residenceService.selectByName(residenceList);
////               if(temp.size() == 0){
////                   residenceService.insertBatch(residenceList);
////               }else{
////                   for(Residence residence : temp){
////                       map.put(String.valueOf(residence.getId()),residence.getProjectName());
////                   }
////
////               }
////
////            }
////        }
//        return  map;
//    }

}
